package com.celesea.framework.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.celesea.framework.vo.DictionaryVo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author JackYuan
 * @create 2017-11-28 15:45
 */
public class DictionaryVoUtil {

    public static List<DictionaryVo> toDictionarys(String json) {
        if (StringKit.isEmpty(json)) {
            return null;
        }
        JSONArray jb = JSON.parseArray(json);
        return jb.stream().map(item -> toDictionary((JSONObject) item)).collect(Collectors.toList());
    }

    public static DictionaryVo toDictionary(String json) {
        if (StringKit.isEmpty(json)) {
            return null;
        }
        JSONObject jb = JSON.parseObject(json);
        return toDictionary(jb);
    }

    public static DictionaryVo toDictionary(JSONObject jb) {
        return new DictionaryVo(jb.getString("id"), jb.getString("name"));
    }

    public static String toJson(List<DictionaryVo> users) {
        if (CollectionKit.isEmpty(users)) {
            return null;
        }
        return JSON.toJSONString(users);
    }
}
